import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';

class FeedbackLogic extends GetxController {
  final titleCtrl = TextEditingController();
  final contentCtrl = TextEditingController();
  final emailCtrl = TextEditingController();
  final phoneCtrl = TextEditingController();
  final category = RxnString();
  final priority = RxnString();

  final categories = const ['bug', 'feature', 'ui', 'other'];
  final priorities = const ['low', 'medium', 'high'];

  RxBool submitting = false.obs;

  Future<void> submit() async {
    final t = titleCtrl.text.trim();
    final c = contentCtrl.text.trim();
    if (t.isEmpty || c.isEmpty) {
      IMViews.showToast('请填写标题和内容');
      return;
    }
    submitting.value = true;
    try {
      await Apis.submitAnonymousFeedback(
        title: t,
        content: c,
        category: category.value,
        priority: priority.value,
        contactEmail: IMUtils.emptyStrToNull(emailCtrl.text.trim()),
        contactPhone: IMUtils.emptyStrToNull(phoneCtrl.text.trim()),
      );
      IMViews.showToast('提交成功，感谢您的反馈！');
      Get.back();
    } catch (e) {
      Logger.print('submit feedback error: $e');
    } finally {
      submitting.value = false;
    }
  }

  @override
  void onClose() {
    titleCtrl.dispose();
    contentCtrl.dispose();
    emailCtrl.dispose();
    phoneCtrl.dispose();
    super.onClose();
  }
}